home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / workbench+shell / q-z / wbpi / source / wbpi.e < prev   
Encoding:
Text File  |  1995-09-28  |  6.8 KB  |  219 lines

  1. /*
  2. *    Workbench Pattern Injector
  3. *
  4. *          September 1995
  5. *
  6. *    Public Domain by Chad Randall
  7. *                     mbissaymssiK Software
  8. *                     crandall@msen.com
  9. *                     http://www.msen.com/~crandall
  10. *
  11. *    No warranty is given for the stability of this program.
  12. *
  13. *
  14. *    Usage:
  15. *
  16. *      CLI:  wbpi source (dest=env:sys/wbpattern.prefs) w|wb|s
  17. *
  18. *       WB:  As a default tool (or shift-clicked tool) from a project icon:
  19. *            WINDOWS|WORKBENCH|SCREEN tooltypes specify what to copy (located in PROJECT icon)
  20. *              (requests (EZREQ) what to copy, if no tooltype is present!)
  21. *
  22. */
  23.  
  24. /* Best viewed with a TAB setting of 2 */
  25.  
  26. /* Requires E typed modules by Jason R. Hulance to compile */
  27.  
  28. MODULE 'exec/nodes','exec/ports','exec/types','exec/memory'
  29. MODULE    'iffparse','libraries/iffparse'
  30. MODULE    'exec/tasks'
  31. MODULE    'asl'
  32. MODULE    'wb','workbench/startup','workbench/workbench'
  33. MODULE    'icon'
  34. MODULE    'dos/dosextens','dos/dos'
  35. MODULE    'prefs/wbpattern'
  36.  
  37. MODULE    'mod/filenames'    -> my custom module, with some filename handling routines.
  38.  
  39. DEF data[4]:LIST            -> Pointer to wbpatternprefs structures
  40. DEF datasize[4]:LIST    -> size of struct
  41.  
  42. PROC main() HANDLE
  43.     DEF args[10]:LIST                     -> cli argument array
  44.     DEF i                                                -> loop counter
  45.     DEF rdarg=0                                     -> ptr to readargs struct
  46.  
  47.     DEF use_wb=FALSE,use_win=FALSE,use_scr=FALSE
  48.  
  49.     DEF fh=0
  50.     DEF iff=0:PTR TO iffhandle
  51.  
  52.     DEF errorstring[1000]:STRING-> A dos error string buffer
  53.     DEF namestr[500]:STRING            -> Our CLI task name.
  54.     DEF str[1000]:STRING                -> A general string storage
  55.     DEF task:PTR TO process            -> Our task/process structure, in case GetProgramName() fails.
  56.     DEF source[1000]:STRING            -> Source file
  57.     DEF dest[1000]:STRING                -> Destination file (defaults to "env:sys/wbpattern.prefs"
  58.  
  59.     DEF olddir
  60.     DEF fh2=0
  61.     DEF wb:PTR TO wbstartup
  62.     DEF wbargs:PTR TO wbarg
  63.     DEF projicon=0:PTR TO diskobject
  64.     DEF dir[500]:STRING
  65.     DEF file[500]:STRING
  66.     DEF name[1000]:STRING
  67.     DEF ezres
  68.  
  69.     IF (KickVersion(39)=0) THEN Raise("KICK")
  70.     IF ((aslbase:=OpenLibrary('asl.library',36))=0) THEN Raise("LIB")
  71.     IF ((iconbase:=OpenLibrary('icon.library',36))=0) THEN Raise("LIB")
  72.     IF ((iffparsebase:=OpenLibrary('iffparse.library',36))=0) THEN Raise("LIB")
  73.  
  74.     StrCopy(dest,'env:sys/wbpattern.prefs')    ->get dest (default)
  75.     IF wbmessage
  76.         wb:=wbmessage
  77.         wbargs:=wb.arglist
  78.         IF (wb.numargs<2) THEN Raise("wbar")        -> REQUIRES at least one project icon!
  79.         collectchunks(dest,TRUE,TRUE,TRUE)
  80.         FOR i:=2 TO wb.numargs
  81.             wbargs:=wbargs+SIZEOF wbarg
  82.             olddir:=CurrentDir(wbargs.lock)    -> Changes to our tool directory
  83.             GetCurrentDirName(dir,490)
  84.             CurrentDir(olddir)                        -> Changes back to the original dir, for petes sake!
  85.             eaddpart(dir,wbargs.name,490)
  86.             IF ((fh2:=Lock(dir,ACCESS_READ))=0) THEN Raise("DOS")
  87.             UnLock(fh2)
  88.             IF (projicon:=GetDiskObject(dir))
  89.                 use_wb:=FALSE
  90.                 use_win:=FALSE
  91.                 use_scr:=FALSE
  92.                 IF (FindToolType(projicon.tooltypes,'WORKBENCH')) THEN use_wb:=TRUE
  93.                 IF (FindToolType(projicon.tooltypes,'WINDOWS')) THEN use_win:=TRUE
  94.                 IF (FindToolType(projicon.tooltypes,'SCREEN')) THEN use_scr:=TRUE
  95.                 FreeDiskObject(projicon)
  96.             ENDIF
  97.             IF ((use_wb OR use_win OR use_scr)=FALSE)
  98.                 ezres:=EasyRequestArgs(0,[20,0,'WBPI','Use which field?','Workbench|Windows|Screen'],0,0)
  99.                 SELECT ezres
  100.                 CASE 0;use_scr:=TRUE
  101.                 CASE 1;use_wb:=TRUE
  102.                 CASE 2;use_win:=TRUE
  103.                 ENDSELECT
  104.             ENDIF
  105.             collectchunks(dir,use_wb,use_win,use_scr)
  106.         ENDFOR
  107.     ELSE
  108.         FOR i:=0 TO 9;args[i]:=0;ENDFOR
  109.         task:=FindTask(0)
  110.         GetProgramName(namestr,490)    -> Get our process name, from the CLI handle.
  111.         IF StrLen(namestr)=0 THEN StrCopy(namestr,task.task.ln.name)    -> Task name, in case above failed
  112.         rdarg:=ReadArgs('FROM/A,TO,WB=WORKBENCH/S,W=WINDOWS/S,S=SCREEN/S',args,0)
  113.         IF rdarg=0 THEN Raise("DOS")
  114.         StrCopy(source,args[0])                                    ->get source
  115.         IF (args[1]) THEN StrCopy(dest,args[1])    ->get dest (user)
  116.         IF (args[2]) THEN use_wb:=TRUE
  117.         IF (args[3]) THEN use_win:=TRUE
  118.         IF (args[4]) THEN use_scr:=TRUE
  119.         IF ((args[2] OR args[3] OR args[4])=FALSE) THEN Raise(ERROR_BAD_TEMPLATE)
  120.         IF ((args[2] AND args[3] AND args[4])=TRUE) THEN Raise(ERROR_BAD_TEMPLATE)
  121.         collectchunks(source,use_wb,use_win,use_scr)
  122.         collectchunks(dest,Not(use_wb),Not(use_win),Not(use_scr))
  123.     ENDIF
  124.     IF ((data[WBP_ROOT]=0) OR
  125.             (data[WBP_DRAWER]=0) OR
  126.             (data[WBP_SCREEN]=0)) THEN Raise("2few")
  127.     IF ((fh:=Open(dest,NEWFILE))=0) THEN Raise("DOS")
  128.     IF ((iff:=AllocIFF())=0) THEN Raise("IFF")
  129.     iff.stream:=fh
  130.     InitIFFasDOS(iff)
  131.     IF (OpenIFF(iff,IFFF_WRITE)) THEN Raise("IFF")
  132.     PushChunk(iff,"PREF","FORM",IFFSIZE_UNKNOWN)
  133.         PushChunk(iff,"PREF","PRHD",6)
  134.             WriteChunkBytes(iff,[0,0]:LONG,6)
  135.         PopChunk(iff)
  136.         FOR i:=0 TO 2
  137.             PushChunk(iff,"PREF","PTRN",datasize[i])
  138.                 WriteChunkBytes(iff,data[i],datasize[i])                    
  139.             PopChunk(iff)
  140.         ENDFOR
  141.     PopChunk(iff)
  142.             /* Let the cleanup routine close the iff! */
  143. EXCEPT DO
  144.     IF rdarg THEN FreeArgs(rdarg)
  145.  
  146.     IF iff THEN CloseIFF(iff)
  147.     IF fh THEN Close(fh)
  148.     IF iff THEN FreeIFF(iff)
  149.  
  150.     SELECT exception
  151.     CASE 0;NOP
  152.     CASE "2few";err('not enough data')
  153.     CASE "wbar";err('WBPI cannot be run alone.\n\nPass icons by shift clicking first.')
  154.     CASE "KICK";err('Sorry.  WBPI requires OS3.0 or greater.')
  155.     CASE "LIB";err('library failed to open')
  156.     CASE "MEM";err('out of memory')
  157.     CASE "IFF";err('iffparse error')
  158.     CASE "DOS"
  159.         IF wbmessage THEN StrCopy(str,'Dos Error:\n')
  160.         StrAdd(str,namestr)
  161.         Fault(IoErr(),str,errorstring,998)
  162.         err(errorstring)
  163.     DEFAULT;
  164.         IF wbmessage THEN StrCopy(str,'Dos Error:\n')
  165.         StrAdd(str,namestr)
  166.         Fault(exception,str,errorstring,998)
  167.         err(errorstring)
  168.     ENDSELECT
  169.     IF aslbase THEN CloseLibrary(aslbase)
  170.     IF iffparsebase THEN CloseLibrary(iffparsebase)
  171.     IF iconbase THEN CloseLibrary(iconbase)
  172. ENDPROC
  173.  
  174. PROC collectchunks(name,usewb,usewin,usescr) HANDLE
  175.     DEF iff=0:PTR TO iffhandle
  176.     DEF ci=0:PTR TO collectionitem
  177.     DEF fh=0
  178.     DEF wbpp:PTR TO wbpatternprefs
  179.     DEF which
  180.  
  181.     IF ((fh:=Open(name,OLDFILE))=0) THEN Raise("DOS")
  182.     IF ((iff:=AllocIFF())=0) THEN Raise("IFF")
  183.     iff.stream:=fh
  184.     InitIFFasDOS(iff)
  185.     IF (OpenIFF(iff,IFFF_READ)) THEN Raise("IFF")
  186.     CollectionChunk(iff,"PREF","PTRN")
  187.     StopOnExit(iff,"PREF","FORM")
  188.     ParseIFF(iff,IFFPARSE_SCAN)
  189.     IF ((ci:=FindCollection(iff,"PREF","PTRN"))=0) THEN Raise("IFF")
  190.     REPEAT
  191.         wbpp:=ci.data
  192.         which:=wbpp.which
  193.         IF (((which=WBP_ROOT) AND (usewb)) OR
  194.                 ((which=WBP_DRAWER) AND (usewin)) OR
  195.                 ((which=WBP_SCREEN) AND (usescr)))
  196.             data[which]:=New(ci.size)                -> E will automatically free these for us, so no Dispose()...
  197.             CopyMem(ci.data,data[which],ci.size)
  198.             datasize[which]:=ci.size
  199.         ENDIF
  200.         ci:=ci.next
  201.     UNTIL ci=0
  202. EXCEPT DO
  203.     IF iff THEN CloseIFF(iff)
  204.     IF fh THEN Close(fh)
  205.     IF iff THEN FreeIFF(iff)
  206.     ReThrow()
  207. ENDPROC
  208.  
  209. PROC err(msgptr)
  210.     IF ((aslbase<>0) AND (wbmessage))
  211.          EasyRequestArgs(0,[20,0,'Error!',msgptr,'Okay'],0,0)
  212.     ELSE
  213.         WriteF('\s\n',msgptr)
  214.     ENDIF
  215. ENDPROC
  216.  
  217. ver:
  218. CHAR    0,0,'$VER: wbpi 1.0 (28.9.95)',10,13,0,0
  219.